package com.leo.ktvplayer.network.http;

import android.util.Log;

import com.leo.ktvplayer.domain.BaseReq;
import com.leo.ktvplayer.util.JsonUtil;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Objects;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by LeoLu on 2017/2/27.
 */
public class HttpManager {
    private static HttpManager manager;
    public OnHttpCall onHttpCall;
    private Object object = new Object();
    public void setHttpCall(OnHttpCall onHttpCall) {
        this.onHttpCall = onHttpCall;
    }

    public HttpManager(){

    }


    public synchronized void sendHttp(BaseReq req,String tag) throws Exception {

            Retrofit retrofit = new Retrofit.Builder().baseUrl("http://192.168.1.212:1234/g/").build();

            HttpService service = retrofit.create(HttpService.class);



            //http://www.baidu.com/g/// STOPSHIP: 2017/5/21 /? getIser=123&username=123&pwd=123;
            HashMap<String, String> map = new HashMap<>();
            service.post("/get",map).enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    try {
                        String result =  response.body().string();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {

                }
            });

            perparMap(req.getClass(), map, req);
            service.callSomething(req.getUrl(), map).enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    if (onHttpCall != null) {
                        try {
                            onHttpCall.onCall(response.body().string(), tag);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {

                }
            });

    }

    /**
     * prepare any data to map ,
     * you can call the method easy to compass your data
     * @param clz
     * @param params
     * @param req
     * @throws Exception
     */
    private void perparMap(Class<?> clz , HashMap<String ,String> params , Object req) throws Exception {
        Field[] fields = clz.getDeclaredFields();

        for (Field f : fields) {
            if (f.getName().contains("$_") || (Modifier.isStatic(f.getModifiers())
                    && Modifier.isFinal(f.getModifiers()))) { // 排除static
                continue;
            }
            f.setAccessible(true);
            String field = f.getName();
            Object val = f.get(req);

            if (val != null) {
                params.put(field, String.valueOf(val));
            }
        }

        clz = clz.getSuperclass();
        if (clz != null) {
            perparMap(clz, params, req);
        }
    }

}
